package com.shishuo.cms.tag;

import static freemarker.template.ObjectWrapper.BEANS_WRAPPER;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.shishuo.cms.entity.ClassesEntity;
import com.shishuo.cms.entity.PictureEntity;
import com.shishuo.cms.entity.VideoEntity;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.vo.ArticleEntityVo;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Service
public class HotListTag extends BaseTagPlugin
{

	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException
	{
		String aliasName = params.get("aliasName").toString();
		int topCount = Integer.parseInt(params.get("top") != null ? params.get("top").toString() : "10");
		try
		{
			ClassesEntity classesEntity = classesService.getByAliasName(aliasName);
			//默认获取热门文章
			if ((null==classesEntity)||classesEntity.getType().equals("article"))
			{
				List<ArticleEntityVo> pageList = articleService.getTopArticle(topCount, aliasName);
				env.setVariable("tag_list_value", BEANS_WRAPPER.wrap(pageList));
			}else if (classesEntity.getType().equals("picture")){
				List<PictureEntity> list = pictureService.getTopPicture(topCount, aliasName);
				env.setVariable("tag_list_value", BEANS_WRAPPER.wrap(list));
			}else if (classesEntity.getType().equals("video")){
				List<VideoEntity> list = videoService.getTopVideo(topCount, aliasName);
				env.setVariable("tag_list_value", BEANS_WRAPPER.wrap(list));
			}
			env.setVariable("tag_class", BEANS_WRAPPER.wrap(classesEntity));
			body.render(env.getOut());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
